<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #result {
        width: 200px;
        height: 100px;
        border: solid 1px pink;
      }
    </style>
  </head>

  <body>
    <h1>胡宏洋发顶会了</h1>
    <button>点击获取数据</button>
    <div id="result"></div>
  </body>
  <script type="text/javascript">
    const btn = document.getElementsByTagName("button")[0];
    const result = document.getElementById("result");
    console.log(btn);
    btn.onclick = function () {
      console.log("执行了");
      const xhr = new XMLHttpRequest();
      xhr.open("GET", "http://127.0.0.1:8000/server");
      xhr.send();
      xhr.onreadystatechange = function () {
        //判断
        if (xhr.readyState === 4) {
          //判断响应状态码
          if (xhr.status >= 200 && xhr.status < 300) {
            //处理结果
            console.log(xhr.status);
            console.log(xhr.statusText);
            console.log(xhr.getResponseHeader); //所有响应头
            console.log(xhr.response); //响应体
            result.innerText = xhr.response;
          } else {
            alert("出错了");
          }
        }
      };
    };
  </script>
</html>
